package com.kaisu.blog.infrastructure.blog.convert.decorated;

import com.kaisu.blog.domain.blog.Blog;
import com.kaisu.blog.domain.file.repository.FileRepository;
import com.kaisu.blog.infrastructure.blog.BlogPO;
import com.kaisu.blog.infrastructure.blog.convert.BlogConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author kaisu
 */
@Component
public abstract class BlogConvertDecorated implements BlogConvert {
    @Autowired
    private BlogConvert delegate;
    @Autowired
    private FileRepository fileRepository;

    @Override
    public Blog toDomain(BlogPO po) {
        Blog domain = delegate.toDomain(po);
        if (Objects.nonNull(po.getFileId())) {
            domain.setCover(fileRepository.findById(po.getFileId()));
        }
        return domain;
    }
}
